//
//  XinJianZongLanKeHuViewController.m
//  JuBoCRM
//
//  Created by iMac on 2024/10/23.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "XinJianZongLanKeHuViewController.h"
#import "BRStringPickerView.h"
@interface XinJianZongLanKeHuViewController (){
    NSArray *region;
    NSArray *source;
}
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomSpace;
@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingScrollView *mainScrollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
@property (weak, nonatomic) IBOutlet UITextField *laiYuanTf;
@property (weak, nonatomic) IBOutlet UITextField *diQuTf;
@property (weak, nonatomic) IBOutlet UITextView *beiZhuTf;
@property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *textFieldArray;

@end

@implementation XinJianZongLanKeHuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
   
    self.customNavBar.title = @"新建客户";
    if (@available(iOS 11.0, *)) {
        self.mainScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    _topSpace.constant = NavaBarHeight;
    _bottomSpace.constant = ScreenBottom+20;
    
    _beiZhuTf.wzb_placeholder = @"请输入备注";
    
    [_phoneTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    [self huoQuShuJu];
}
-(void)textFieldDidChange:(UITextField *)textField
{
    CGFloat maxLength = 11;
    NSString *toBeString = textField.text;
    
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position || !selectedRange){
        if (toBeString.length > maxLength)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:maxLength];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
}


- (void)huoQuShuJu{
    [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/crmCustomerAll/getSelect" andParas:nil andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if (!error) {
            NSLog(@"info:%@",info);
            NSDictionary *dataDic = [info objectForKey:@"data"];
            region = [dataDic objectForKey:@"region"];
            source = [dataDic objectForKey:@"source"];
        }
    }];
}
//来源
- (IBAction)laiYuan:(id)sender {
    [self xuanZeShuJuIsLaiYuan:YES];
}
//地区
- (IBAction)iQu:(id)sender {
    [self xuanZeShuJuIsLaiYuan:NO];
}
//保存
- (IBAction)baoCun:(id)sender {
    [self.view endEditing:YES];
    for (UITextField *tf in _textFieldArray) {
        if (tf.text.length == 0) {
            [ZZPUtils showWarning:tf.placeholder];
            return;
        }
    }
    
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    NSMutableDictionary *entity = [NSMutableDictionary dictionary];
    [entity setValue:_phoneTf.text forKey:@"mobile"];
    [entity setValue:_beiZhuTf.text forKey:@"remark"];
    [entity setValue:_diQuTf.text forKey:@"region"];
    for (NSDictionary *tempD in source) {
        if ([GetHandleString(tempD, @"name") isEqualToString:_laiYuanTf.text]) {
            [entity setValue:GetHandleString(tempD, @"value") forKey:@"source"];
        }
    }
//    [entity setValue:_laiYuanTf.text forKey:@"source"];
    [paraDic setValue:entity forKey:@"entity"];
    [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/crmCustomerAll/addOrUpdate" andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if (!error) {
            [ZZPUtils showWarning:@"创建成功"];
            if (_manageClick) {
                _manageClick(0);
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}


- (void)xuanZeShuJuIsLaiYuan:(BOOL)isLaiYuan{
    [self.view endEditing:YES];
    BRStringPickerView *datePickerView = [[BRStringPickerView alloc]init];
    datePickerView.pickerMode = BRStringPickerComponentSingle;
    datePickerView.title = @"";
    datePickerView.dataSourceArr = region;
    datePickerView.selectValue = _diQuTf.text;
    if (isLaiYuan) {
        NSMutableArray *array = [NSMutableArray array];
        for (NSDictionary *tempD in source) {
            [array addObject:GetHandleString(tempD, @"name")];
        }
        datePickerView.dataSourceArr = array;
        datePickerView.selectValue = _laiYuanTf.text;
    }
    
    datePickerView.resultModelBlock = ^(BRResultModel *resultModel) {
        NSLog(@"选择的索引：%@", @(resultModel.index));
        NSLog(@"选择的值：%@", resultModel.value);
        
        
        if (isLaiYuan) {
            _laiYuanTf.text = resultModel.value;
        }else{
            _diQuTf.text = resultModel.value;
        }
    };
    
    BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
    customStyle.pickerColor = [UIColor whiteColor];
    customStyle.selectRowTextColor = rgba(40, 120, 255, 1);
    customStyle.titleBarColor = [UIColor whiteColor];
    customStyle.titleTextColor = [UIColor whiteColor];
    customStyle.cancelTextColor = rgba(153, 153, 153, 1);
    customStyle.doneTextColor = rgba(40, 120, 255, 1);
    customStyle.titleBarHeight = 48;
    customStyle.titleTextFont = [UIFont systemFontOfSize:18];
    datePickerView.pickerStyle = customStyle;
    customStyle.topCornerRadius = 10;
    customStyle.hiddenTitleLine = YES;
    customStyle.separatorColor = rgba(223, 229, 242, 1);
    customStyle.separatorHeight = 0.5;
    customStyle.rowHeight = 64;
    
    [datePickerView show];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
